/*
   Copyright (c) 2023 李伟国
   learner-platform is licensed under Mulan PSL v2.
   You can use this software according to the terms and conditions of the Mulan PSL v2. 
   You may obtain a copy of Mulan PSL v2 at:
               http://license.coscl.org.cn/MulanPSL2 
   THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND, EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT, MERCHANTABILITY OR FIT FOR A PARTICULAR PURPOSE.  
   See the Mulan PSL v2 for more details. 
*/

package cn.platform.service.impl;

import cn.platform.constant.AlgorithmSetConstant;
import cn.platform.entity.AlgorithmSet;
import cn.platform.mapper.AlgorithmSetMapper;
import cn.platform.page.PageFactory;
import cn.platform.service.IAlgorithmSetService;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.stereotype.Service;


@Service
public class AlgorithmSetServiceImpl extends ServiceImpl<AlgorithmSetMapper, AlgorithmSet>
        implements IAlgorithmSetService, AlgorithmSetConstant {
    /**
     * 创建题目集，从数据库中创建一条记录，并将题目集状态改为draft
     *
     * @param algorithmSet 创建的题目集
     * @return 操作是否成功
     */
    @Override
    public boolean createAlgorithmSet(AlgorithmSet algorithmSet) {
        algorithmSet.setStatus(STATUS_DRAFT);
        int insert = this.baseMapper.insert(algorithmSet);
        return insert > 0;
    }

    /**
     * 发布题目集，将题目集的状态改为release
     *
     * @param algorithmSet 题目集
     * @return 操作是否成功
     */
    @Override
    public boolean releaseAlgorithmSet(AlgorithmSet algorithmSet) {
        algorithmSet.setStatus(STATUS_RELEASED);
        int i = this.baseMapper.updateById(algorithmSet);
        return i>0;
    }

    /**
     * 删除题目集，将题目集的状态改为deleted
     *
     * @param algorithmSet 题目集
     * @return 操作是否成功
     */
    @Override
    public boolean deleteAlgorithmSet(AlgorithmSet algorithmSet) {
        algorithmSet.setStatus(STATUS_DELETED);
        int i = this.baseMapper.updateById(algorithmSet);
        return i>0;
    }

    /**
     * 停用题目集，将题目集的状态更改为locked
     *
     * @param algorithmSet 题目集
     * @return 操作是否成功
     */
    @Override
    public boolean lockAlgorithmSet(AlgorithmSet algorithmSet) {
        algorithmSet.setStatus(STATUS_LOCKED);
        int i = this.baseMapper.updateById(algorithmSet);
        return i > 0;
    }

    /**
     * 按照题目集名称模糊查找题目集， 如果name为空，则返回所有题目集
     *
     * @param name   题目集名称（如有，启用模糊搜索）
     * @param status 题目集状态
     * @param userId 作者id
     * @return 题目集列表
     */
    @Override
    public Page<AlgorithmSet> algorithmSetList(String name, String status, Long userId) {
        return this.baseMapper.getAlgorithmSetList(PageFactory.defaultPage(), name, status,userId);
    }
}
